<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script src="./js/03-ajax.js"></script>
  <script>
    // const promises = [
    //   ajax('http://localhost:3000/posts'),
    //   new Promise((resolve, reject) => {
    //     setTimeout(() => {
    //       reject('请求超时')
    //     }, 2000)
    //   })
    // ];

    // Promise.race(promises).then(value => {
    //   console.log(value)
    // }).catch(error => {
    //   console.log(error)
    // })

    function query(url, delay=2000) {
      let promises = [
        ajax(url),
        new Promise((resolve, reject) => {
          setTimeout(() => {
            reject('请求超时')
          }, delay)
        })
      ]
      return Promise.race(promises)
    }
    query('http://localhost:3000/posts').then(value => {
      console.log(value)
    })

    // const p1 = new Promise((resolve, reject) => {
    //   setTimeout(() => {
    //     // reject("fail")
    //     resolve("第一个异步")
    //   }, 1000)
    // })
    // const p2 = new Promise((resolve, reject) => {
    //   setTimeout(() => {
    //     resolve("第二个异步")
    //   },800)
    // })
    // // 哪个返回的块就执行哪个
    // Promise.race([p1, p2]).then(result => {
    //   console.log(result) 
    // }).catch(error => {
    //   console.log(error)
    // })
  </script>
</body>
</html>